package ajax17;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class mem_login {
	String userid;
	String nickname;
	String pw1;
	String pw2;
	String email;
	String useridmsg;
	String nicknamemsg;
	String pw1msg;
	String pw2msg;
	String emailmsg;
	String loginmsg;
	private boolean login;
	private int return_time;
	
	String referer;
	
	public boolean login() {
		PreparedStatement pstmt = null;
		Connection conn = null;
		login = false;
		
		try {
			DriverManager.registerDriver(new Driver());
			conn = DriverManager.getConnection(
					"jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
			String sql = "select * from users where id = ? and password = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userid);
			pstmt.setString(2, pw1);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()) {
				login = true;
				nickname = rs.getString("nickname");
			} else {
				loginmsg = "登入失敗!!";
				return_time = return_time + 1;
				System.out.println("return_time = " + return_time);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if (conn != null) {
				try {
					pstmt.close();
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return login;
	}
	
	public mem_login(){
		
	}
	
	protected mem_login(String userid, String pw1) {
		super();
		this.userid = userid;
		this.pw1 = pw1;
	}
	
	

	protected mem_login(String userid, String pw1, int return_time) {
		super();
		this.userid = userid;
		this.pw1 = pw1;
		this.return_time = return_time;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getPw2() {
		return pw2;
	}

	public void setPw2(String pw2) {
		this.pw2 = pw2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUseridmsg() {
		return useridmsg;
	}

	public void setUseridmsg(String useridmsg) {
		this.useridmsg = useridmsg;
	}

	public String getNicknamemsg() {
		return nicknamemsg;
	}

	public void setNicknamemsg(String nicknamemsg) {
		this.nicknamemsg = nicknamemsg;
	}

	public String getPw1msg() {
		return pw1msg;
	}

	public void setPw1msg(String pw1msg) {
		this.pw1msg = pw1msg;
	}

	public String getPw2msg() {
		return pw2msg;
	}

	public void setPw2msg(String pw2msg) {
		this.pw2msg = pw2msg;
	}

	public String getEmailmsg() {
		return emailmsg;
	}

	public void setEmailmsg(String emailmsg) {
		this.emailmsg = emailmsg;
	}

	public boolean isLogin() {
		return login;
	}

	public void setLogin(boolean login) {
		this.login = login;
	}

	public String getLoginmsg() {
		return loginmsg;
	}

	public void setLoginmsg(String loginmsg) {
		this.loginmsg = loginmsg;
	}

	public String getReferer() {
		if (referer!=null && referer.substring(referer.lastIndexOf("/")).equals("/index.jsp")){
            referer = null;
    }
		return referer;
	}

	public void setReferer(String referer) {
		this.referer = referer;
	}

	public int getReturn_time() {
		return return_time;
	}

	public void setReturn_time(int return_time) {
		this.return_time = return_time;
	}

	
}

